package com.easpeed

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.telephony.TelephonyManager
import android.text.TextUtils
import android.util.Log;
import com.bigyu.utils.Logger
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.ToastUtils
import com.easpeed.common.CommonConsts
import com.easpeed.comon.consts.ConstsApi
import com.easpeed.comon.memorycache.CommonMemoryCache
import com.easpeed.comon.utils.CacheHelper
import com.easpeed.comon.utils.CommonOperateUtils
import com.easpeed.comon.utils.SignalStrengthListener
import com.easpeed.comon.utils.SocketHelper
import com.easpeed.comon.utils.isUser
import com.easpeed.comon.utils.permissionRequest
import com.easpeed.comon.utils.processEmpty
import com.easpeed.elevator.R
import com.easpeed.elevator.base.MyApplication.context
import com.easpeed.elevator.ui.MainActivity
import com.easpeed.user.UserBaseActivity
import com.easpeed.user.UserLoginActivity
import com.easpeed.user.UserMainActivity
import com.tencent.bugly.crashreport.CrashReport
import java.util.UUID

class SplashActivity : UserBaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_splash)

    }

    /**
     * 通过资源id获取状态栏的高度
     *
     * @return int
     */
    @SuppressLint("InternalInsetResource")
    fun getStatusHeight(): Int {
        //通过资源的id，获取id资源
        val resources = resources
        val resId = resources.getIdentifier("status_bar_height", "dimen", "android")
        return resources.getDimensionPixelOffset(resId)
    }

    fun getStatusBarHeightCompat(activity: Activity): Int {
        var statusBarHeight = 0
        val decorView = activity.window.decorView
        decorView.setOnApplyWindowInsetsListener { _, insets ->
            statusBarHeight = insets.systemWindowInsetTop
            insets
        }
        return statusBarHeight
    }
    fun getSignalStrength(context: Context): Int {
        val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val signalStrength = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            telephonyManager.signalStrength
        } else {
            TODO("VERSION.SDK_INT < P")
        }
        return signalStrength?.level ?: -1 // 信号强度等级，0-4，-1 表示无法获取
    }
    override fun initView() {
        CrashReport.initCrashReport(getApplicationContext(), "7030a6b483", false);

//        Logger.e("TAG", "initView:${BarUtils.getStatusBarHeight(mContext.applicationContext)} ")
//        Logger.e("TAG", "initView222:${getStatusHeight()} ")

        Logger.e("TAG", "--------------------initView: ${SocketHelper.generateUniqueIdFromUUID()}")
        //生成一个随机的设备id
        val deviceId = sp.getString("deviceID", "")
        if (deviceId.isNullOrEmpty()) {
            val mDeviceId = SocketHelper.generateUniqueIdFromUUID()
            CommonConsts.deviceId = mDeviceId
            editor.putString("deviceID", mDeviceId)
        } else {
            CommonConsts.deviceId = deviceId
        }

        ConstsApi.socketData = UUID.randomUUID().toString()
        CommonConsts.targetDeviceId = sp.getString("targetDeviceId", "").processEmpty(defaultStr = "").toString()


        CommonMemoryCache.deviceName = sp.getString("deviceName", "")
        CommonMemoryCache.deviceModel = sp.getString("deviceModel", "")

        if (TextUtils.isEmpty(CommonMemoryCache.deviceName)) {
            CommonMemoryCache.deviceName = DeviceUtils.getModel()
            editor.putString("deviceName", CommonMemoryCache.deviceName)
            editor.commit()
        }
        if (TextUtils.isEmpty(CommonMemoryCache.deviceModel)) {
            CommonMemoryCache.deviceModel = DeviceUtils.getModel()
            editor.putString("deviceModel", CommonMemoryCache.deviceModel)
            editor.commit()
        }

        initApp()
    }


    private fun initApp() {
        //        判断是否登录
        val  boolean = CacheHelper.instance().getBooleanValue("isLogin", false)
        if (isUser()) {
            if (boolean) {
                CommonOperateUtils.moveTo(this, UserMainActivity::class.java)
            } else {
                CommonOperateUtils.moveTo(this, UserLoginActivity::class.java)
            }
        } else {
            // 设备端
            CommonConsts.targetDeviceId = CommonConsts.deviceId
            if (boolean) {
//                CommonOperateUtils.moveTo(this, UserMainActivity::class.java)
                CommonOperateUtils.moveTo(this, MainActivity::class.java)
            } else {
                CommonOperateUtils.moveTo(this, UserLoginActivity::class.java)
            }
        }

        SocketHelper.initAppType()
        finish()
    }
}